﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

/*
 * 日期帮助类
 * William
 * 2010年11月5日
 */

namespace Utility
{
    public class FileHelper
    {
        public static FileHelper Instance = new FileHelper();

        private FileHelper() { }

        public byte[] StreamToBytes(Stream stream)
        {
            List<byte> bytes = new List<byte>();
            int temp = stream.ReadByte();
            while (temp != -1)
            {
                bytes.Add((byte)temp);
                temp = stream.ReadByte();
            }

            return bytes.ToArray();
        }

        public bool FileExists(string path)
        {
            try
            {
                return File.Exists(path);
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool CreateFile(string path, string name, string content)
        {
            try
            {
                path = CheckPath(path, true);
                CreateDirectory(path);
                if (File.Exists(path + name))
                {
                    // Note that no lock is put on the
                    // file and the possibliity exists
                    // that another process could do
                    // something with it between
                    // the calls to Exists and Delete.
                    File.Delete(path + name);
                }
                // Create the file.
                FileStream fs = File.Create(path + name);
                byte[] info = new UTF8Encoding(true).GetBytes(content);
                info = Encoding.Convert(Encoding.UTF8, Encoding.Default, info);
                // Add some information to the file.
                fs.Write(info, 0, info.Length);
                fs.Close();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        //CreateFile

        public bool SaveFile(string path, string name, string content)
        {
            try
            {
                path = CheckPath(path, true);
                CreateDirectory(path);
                SetFilesAttributesToArchive(path + name);
                // Create the file.
                FileStream fs = File.Open(path + name, FileMode.Append);
                byte[] info = new UTF8Encoding(true).GetBytes(content);
              
                // Add some information to the file.
                fs.Write(info, 0, info.Length);
                fs.Close();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool SaveFile(string path, string name, byte[] info, FileMode fm)
        {
            try
            {
                path = CheckPath(path, true);
                CreateDirectory(path);
                SetFilesAttributesToArchive(path + name);
                // Create the file.
                FileStream fs = File.Open(path + name, fm);
                fs.Write(info, 0, info.Length);
                fs.Close();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        //CreateFile
        public bool SaveFile(string path, string name, string content, FileMode fm)
        {
            try
            {
                path = CheckPath(path, true);
                CreateDirectory(path);
                SetFilesAttributesToArchive(path + name);
                // Create the file.
                FileStream fs = File.Open(path + name, fm);
                byte[] info = new UTF8Encoding(true).GetBytes(content);
                //info = Encoding.Convert(Encoding.UTF8, Encoding.Default, info);
                // Add some information to the file.
                fs.Write(info, 0, info.Length);
                fs.Close();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        //CreateFile

        public bool MoveFile(string currentPath, string fileName, string targetPath)
        {
            try
            {
                currentPath = CheckPath(currentPath, true);
                targetPath = CheckPath(targetPath, true);
                CreateDirectory(targetPath);
                if (!File.Exists(currentPath + fileName))
                {
                    return false;
                }
                File.Move(currentPath + fileName, targetPath + fileName);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        //MoveFile

        public bool MoveFile(string fileName, string targetPath)
        {
            try
            {
                targetPath = CheckPath(targetPath, true);
                CreateDirectory(targetPath);
                if (!File.Exists(fileName))
                {
                    return false;
                }
                File.Move(fileName, targetPath + fileName);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        //MoveFile

        public bool CopyFile(string currentPath, string fileName, string targetPath)
        {
            try
            {
                currentPath = CheckPath(currentPath, true);
                targetPath = CheckPath(targetPath, true);
                CreateDirectory(targetPath);
                if (!File.Exists(currentPath + fileName))
                {
                    return false;
                }
                File.Copy(currentPath + fileName, targetPath + fileName);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        //CopyFile

        public bool DeleteFile(string path, string name)
        {
            try
            {
                path = CheckPath(path, true);
                if (File.Exists(path + name))
                {
                    File.Delete(path + name);
                }
                else
                {
                    return false;
                }
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        //DeleteFile

        public string[] FindFile(string path, string fileName)
        {
            string[] filePath = null;
            try
            {
                // Determine whether the directory exists.
                if (Directory.Exists(path))
                {
                    filePath = Directory.GetFiles(path, fileName, SearchOption.AllDirectories);
                }

                return filePath;
            }
            catch (Exception e)
            {
                return filePath;
            }
        }

        //CreateDirectory

        public bool CopyFile(string currentPath, string fileName, string targetPath, string targetName)
        {
            try
            {
                currentPath = CheckPath(currentPath, true);
                targetPath = CheckPath(targetPath, true);
                CreateDirectory(targetPath);
                if (!File.Exists(currentPath + fileName))
                {
                    return false;
                }
                File.Copy(currentPath + fileName, targetPath + targetName);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        //CopyFile

        public bool CreateDirectory(string path)
        {
            try
            {
                // Determine whether the directory exists.
                if (!Directory.Exists(path))
                {
                    // Try to create the directory.
                    Directory.CreateDirectory(path);
                }
                else
                {
                    return false;
                }

                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        //CreateDirectory

        public bool RenameDirectory(string oldPath, string newPath)
        {
            try
            {
                oldPath = CheckPath(oldPath, true);
                newPath = CheckPath(newPath, true);

                if (!Directory.Exists(oldPath))
                {
                    return true;
                }

                Directory.Move(oldPath, newPath);

                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        //MoveDirectory

        public bool RenameFile(string path, string oldFileName, string newFileName)
        {
            try
            {
                path = CheckPath(path, true);

                if (!Directory.Exists(path))
                {
                    return false;
                }

                if (!File.Exists(path + oldFileName))
                {
                    return false;
                }

                if (File.Exists(path + newFileName))
                {
                    return false;
                }
                File.Move(path + oldFileName, path + newFileName);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        //MoveDirectory

        public bool MoveDirectory(string path1, string path2)
        {
            try
            {
                //path1 = Function.CheckPath(path1, True)
                //path2 = Function.CheckPath(path2, True)

                //If Not Directory.Exists(path1) Then
                //    Return True
                //End If

                //CreateDirectory(path2)

                //If Directory.GetFiles(path1, "*", SearchOption.AllDirectories).Length > 0 _
                //Or Directory.GetDirectories(path1, "*", SearchOption.AllDirectories).Length > 0 Then
                //    Directory.Move(path1, path2)
                //End If

                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        //MoveDirectory

        public bool CopyDirectory(string path1, string path2)
        {
            try
            {
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        //CopyDirectory

        public bool DeleteDirectory(string path)
        {
            try
            {
                // Determine whether the directory exists.
                if (Directory.Exists(path))
                {
                    if (!SetFilesAttributesToArchive(path))
                    {
                        return false;
                    }
                    // Try to remove all directories,subdirectories,and files in path
                    Directory.Delete(path, true);
                }

                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        //DeleteDirectory

        public bool IsDirectory(string path)
        {
            try
            {
                //' Determine whether the directory exists.
                //If directory.Exists(path) Then
                //    If Not SetFilesAttributesToArchive(path) Then
                //        Return False
                //    End If
                //    ' Try to remove all directories,subdirectories,and files in path
                //    directory.Delete(path, True)
                //End If

                return Directory.Exists(path);
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public string[] GetFiles(string path)
        {
            string[] files = null;
            try
            {
                if (IsDirectory(path))
                {
                    files = Directory.GetFiles(path);
                }
                return files;
            }
            catch (Exception e)
            {
                return files;
            }
        }

        public string[] GetAllFiles(string path)
        {
            string[] files = null;
            try
            {
                if (IsDirectory(path))
                {
                    files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);
                }
                return files;
            }
            catch (Exception e)
            {
                return files;
            }
        }

        public string GetDirectoryName(string path)
        {
            path = CheckPath(path, true);
            string[] s = path.Replace("\\\\", "\\").Split('\\');
            return s[s.Length - 2].ToString();
        }

        //GetDirectoryName

        public string ReadFile(string path)
        {
            try
            {
                if (!File.Exists(path))
                {
                    return "";
                }
                return Encoding.UTF8.GetString(File.ReadAllBytes(path));
            }
            catch (Exception ex)
            {
                return "";
            }
        }

        //ReadFile

        public byte[] ReadFileBytes(string path)
        {
            try
            {
                if (!File.Exists(path))
                {
                    return null;
                }
                return File.ReadAllBytes(path);
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        //ReadFile

        public bool SetFilesAttributesToArchive(string path)
        {
            try
            {
                string dir = path;
                string[] fs = Directory.GetFiles(dir, "*.*", SearchOption.AllDirectories);
                string f = null;
                foreach (string f_loopVariable in fs)
                {
                    f = f_loopVariable;
                    File.SetAttributes(f, FileAttributes.Archive);
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        //SetFilesAttributesToArchive

        public bool SetFilesAttributesToArchive(string path, string name)
        {
            try
            {
                if (!File.Exists(path + name))
                {
                    return false;
                }
                File.SetAttributes(path + name, FileAttributes.Archive);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        //SetFilesAttributesToArchive

        private string CheckPath(string Path, bool flg)
        {
            try
            {
                string tmp_Path = Path;
                string tmp = tmp_Path.Remove(0, tmp_Path.Length - 1);
                if (flg)
                {
                    if (!tmp.Equals("\\"))
                    {
                        return Path + "\\";
                    }
                }
                else
                {
                    if (tmp.Equals("\\"))
                    {
                        return Path.Remove(Path.Length - 1, 1);
                    }
                }
                return Path;
            }
            catch (Exception ex)
            {
                return Path;
            }
        }
    }
}